package 二叉树;

public class 二叉树的最小深度 {
    public class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    //与最大深度的算不有点不一样
    //不一样的是因为最小深度是从根节点到最近**叶子节点**的最短路径上的节点数量 意思就是说不能简单的直接递归加一，要判断左右子树的null情况
    public int mindepth(TreeNode root){
        if (root == null) return 0;
        int leftdepth = mindepth(root.left);
        int rightdepth = mindepth(root.right);
        if (root.left == null) return rightdepth+1;
        if (root.right == null) return leftdepth+1;
        return Math.min(rightdepth,leftdepth);
    }
}
